﻿Imports System.Threading
Imports Newtonsoft.Json.Linq
Imports System.Net
Imports System.IO
Imports System.Runtime.InteropServices

Public Class frmMain

    Public is_debug As Boolean = True

    Public Account As New clsAccount
    Public FriendFB As New clsFriend
    Public MessageFB As New clsMessage
    Public GroupFB As New clsGroup


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Account.Login.Logout()
        Application.DoEvents()
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        Me.Width = 1000
        Me.Height = 600
        frmDebug.Show()
    End Sub


    Sub ResetForm()
        cmdLogin.Enabled = True
        lblTotalFriend.Text = "..."
        panelLoged.Height = 0
        panelLogin.Height = 106
    End Sub



    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
        cmdLogin.Enabled = False
        Account.Login.Login(txtUsername.Text, txtPassword.Text)
    End Sub



    Private Sub cmdLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogout.Click
        Account.Login.Logout()
        ResetForm()
    End Sub

    Private Sub timUpdateFriendCount_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timUpdateFriendCount.Tick
        If (Account.Login.infoLogin.FacebookID > 0) Then
            Debug.Print("Auto update friend count")
            FriendFB.UpdateFriendCount()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GroupFB.JoinGroup.JoinGroup(236891893140383)
    End Sub

    Public Function get_url(ByVal url)
        Debug.Print(url)
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Try
            request = DirectCast(WebRequest.Create(url), HttpWebRequest)
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())

            Dim rawresp As String
            rawresp = reader.ReadToEnd()
            If rawresp <> "" Then
                Return rawresp
            End If
        Catch ex As Exception

        Finally

        End Try
        Return False
    End Function

    Public Sub test(ByVal callback As Action(Of String, String))
        callback("aaaa", "vvv")
    End Sub

    Public Sub callback1(ByVal test As String, ByVal test1 As String)
        Debug.Print(test & "-" & test1)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        test(callback:=AddressOf callback1)

    End Sub

    Private Sub timUpdateMessageUnread_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timUpdateMessageUnread.Tick
        MessageFB.getMessageUnread()
    End Sub

    Private Sub timCheckSendMessageUnread_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timCheckSendMessageUnread.Tick
        If (MessageFB.DictMessageUnread.Count > 0) Then
            Dim item As objMessageReceived
            item = MessageFB.DictMessageUnread.Item(MessageFB.DictMessageUnread.First.Key)
            If (item.Processing = False) Then
                item.Processing = True
                Dim ListSend As New ArrayList
                ListSend.Add(item.SenderID)
                MessageFB.MessageComposer.SendMessage(ListSend, MessageFB.getMessageContentSendUnread(item.Message), cback:=AddressOf MessageFB.MessageComposerUnread_Success)
            End If

        End If
    End Sub

    Private Sub cmdFindGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFindGroup.Click
        Dim objFind As New objFindGroup
        objFind.GroupName = txtFindGroup.Text
        objFind.MemberCount = txtFilterMemberCount.Text
        objFind.Limit = cboGroupFindLimit.Text
        objFind.Offset = txtGroupFindOffset.Text
        GroupFB.FindGroup.Find(objFind)
    End Sub

    Private Sub cmdClearResultFindGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearResultFindGroup.Click
        lstFindGroup.Items.Clear()
        GroupFB.GroupFindResult.Clear()
    End Sub

    Private Sub cmdDeleteFindGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteFindGroup.Click
        For lCount = lstFindGroup.Items.Count - 1 To 0 Step -1
            If lstFindGroup.Items(lCount).Selected Then
                GroupFB.GroupFindResult.Remove(lstFindGroup.Items(lCount).SubItems(3).Text)
                lstFindGroup.Items(lCount).Remove()
            End If
            Application.DoEvents()
        Next
        UpdateNumberListview(lstFindGroup)
    End Sub

    Sub UpdateNumberListview(ByVal lst As BufferedListview)
        For lCount = lst.Items.Count - 1 To 0 Step -1
            lst.Items(lCount).SubItems(0).Text = lCount + 1
        Next
    End Sub

    Private Sub cmdJoinGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdJoinGroup.Click
        timJoinGroup.Enabled = True
    End Sub

    Private Sub timJoinGroup_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timJoinGroup.Tick
        Debug.Print("join group")
    End Sub
End Class


